home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5639 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  816 b 

  1. Path: news1.radix.net!news
  2. From: jfw@radix.net (Jim Ward)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How do I round and truncate floats to integers?
  5. Date: 20 Feb 1996 02:18:01 GMT
  6. Organization: RadixNet Internet Services
  7. Message-ID: <4gbb0p$c7s@news1.radix.net>
  8. References: <4g009b$c2n@news.tuwien.ac.at>
  9. NNTP-Posting-Host: dialin4.annex1.radix.net
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In article <4g009b$c2n@news.tuwien.ac.at>, sor@rs6.iaee.tuwien.ac.at says...
  15. >Given a float (double, to be exact) how do I 
  16. >
  17. >1) round it to nearest integer (forget 0.5 problem for the moment) and
  18.  
  19. A technique that also works for both positive and negative numbers is:
  20.  
  21.     (int) x < 0.0 ? ceil(x - 0.5) : floor(x + 0.5);
  22.  
  23. I got this from p.135 of Plauger's The Standard C Library.
  24.  
  25.